package com.xaicode.learn.nio;

import lombok.extern.slf4j.Slf4j;

import java.nio.IntBuffer;

/**
 * @author Locker xaicode@sina.com
 * @version 1.0
 * @date 2021/6/13 0013 0:53
 */
@Slf4j
public class BasicBuffer {

    public static void main(String[] args) {

        // create buffer
        IntBuffer intBuffer = IntBuffer.allocate(10);

        for (int i = 0; i < intBuffer.capacity() - 1 /* for testing buffer limit/position */; i++) {
            intBuffer.put(i);
        }

        // flip read/write status
        /*
            public final Buffer flip() {
                limit = position;
                position = 0;
                mark = -1;
                return this;
            }
         */
        log.info("before flip() position=[{}], limit=[{}], mark=[{}]",
                intBuffer.position(), intBuffer.limit(), intBuffer.mark());
        intBuffer.flip();
        log.info("after flip() position=[{}], limit=[{}], mark=[{}]",
                intBuffer.position(), intBuffer.limit(), intBuffer.mark());

        while (intBuffer.hasRemaining()) {
            log.info("read int buffer position=[{}], limit=[{}], value=[{}]",
                    intBuffer.position(), intBuffer.limit(), intBuffer.get());
        }

    }

}
